import 'package:flutter/material.dart';
import 'package:wan_android/pages/page_found.dart';
import 'package:wan_android/pages/page_home.dart';
import 'package:wan_android/pages/page_mine.dart';
import 'package:wan_android/pages/page_search.dart';

///  主页面  主要展示 所有的页面 包括 首页  发现  我的
class MainPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return MainPageState();
  }
}

class MainPageState extends State<MainPage> {
  //文章标题
  var _appBarTitles = ['首页', '发现', '我的'];
  int _index = 0;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text(
          _appBarTitles[_index],
          style: TextStyle(
            color: Colors.white,
            fontSize: 22.0,
          ),
        ),
        actions: <Widget>[
          //在右边添加新的 小控件
          IconButton(
            icon: Icon(
              Icons.search,
              color: Colors.blue,
            ),
            onPressed: _jumpToSearch,
          ),
        ],
      ),
      body: Center(
        child: IndexedStack(
          children: _widget,
          index: _index,
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: <BottomNavigationBarItem>[
          _getBarItem(Icons.home, _appBarTitles[0]),
          _getBarItem(Icons.widgets, _appBarTitles[1]),
          _getBarItem(Icons.person, _appBarTitles[2]),
        ],
        fixedColor: Colors.blue,
        currentIndex: _index,
        iconSize: 30.0,
        onTap: _clickChange,
      ),
    );
  }

  var _widget = [
    PageHome(),
    PageFound(),
    PagePerson(),
  ];

  //点击 底部item
  _clickChange(int index) {
    if (index != _index) {
      setState(() {
        _index = index;
      });
    } else {
      print('请勿重复点击');
    }
  }

  ///返回一个 底部状态栏的item
  BottomNavigationBarItem _getBarItem(IconData iconData, String title) {
    return BottomNavigationBarItem(
      icon: Icon(iconData),
      title: Text(title),
    );
  }

  ///跳转 至搜索界面 使用动态路由 去做
  void _jumpToSearch() {
    Navigator.of(context).push(MaterialPageRoute(builder: (context) {
      return PageSearch(
        editStr: "",
      );
    }));
  }
}
